{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-23T06:59:09.472158Z",
     "start_time": "2024-07-23T06:59:09.464855Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from reportlab.pdfgen import canvas  \n",
    "from reportlab.lib.pagesizes import letter  \n",
    "  \n",
    "# 假设的PDF页面尺寸和边距  \n",
    "width, height = letter  \n",
    "margin = 72  # 假设边距为1英寸（在PDF中，1英寸=72点）  \n",
    "print(width, height)\n",
    "# 图片的尺寸和间距  \n",
    "img_width, img_height = 90, 90  # 图片尺寸（假设为90x90点）  \n",
    "img_spacing = 5  # 图片之间的间距  \n",
    "  \n",
    "# 计算每行可以放置的图片数量  \n",
    "max_images_per_row = (width - 2 * margin) / 8 \n",
    "print(max_images_per_row)\n",
    "# if max_images_per_row > 8:  \n",
    "#     max_images_per_row = 8  # 我们想要每行8个图片  \n",
    "#   \n",
    "# # 创建PDF文件  \n",
    "# c = canvas.Canvas(\"output.pdf\", pagesize=letter)  \n",
    "#   \n",
    "# # 假设我们有足够的图片来填充几行  \n",
    "# image_files = ['image1.jpg', 'image2.jpg', ..., 'imageN.jpg']  # 你的图片文件列表  \n",
    "#   \n",
    "# y_pos = height - margin  # 开始从上往下放置图片  \n",
    "# row_count = 0  \n",
    "  \n",
    "# for img_file in image_files:  \n",
    "#     if row_count == 0:  \n",
    "#         x_pos = margin  # 每行开始时重置x位置  \n",
    "#       \n",
    "#     # 绘制图片  \n",
    "#     c.drawImage(img_file, x_pos, y_pos - img_height, width=img_width, height=img_height, mask='auto')  \n",
    "#       \n",
    "#     # 更新x位置以放置下一张图片  \n",
    "#     x_pos += img_width + img_spacing  \n",
    "#       \n",
    "#     # 如果达到每行的最大图片数，则移动到下一行  \n",
    "#     if (row_count + 1) % max_images_per_row == 0:  \n",
    "#         y_pos -= img_height + img_spacing  # 移动到下一行  \n",
    "#         row_count = 0  # 重置行计数器  \n",
    "#     else:  \n",
    "#         row_count += 1  \n",
    "#   \n",
    "# # 保存PDF  \n",
    "# c.save()"
   ],
   "id": "c251949530590f6f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "612.0 792.0\n",
      "58.5\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-24T01:45:16.977278Z",
     "start_time": "2024-07-24T01:45:16.741650Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from core.PDF import PDF\n",
    "\n",
    "\n",
    "pdf = PDF('/Users/sanghaobo/Desktop', '/Users/sanghaobo/Desktop/test.pdf')\n",
    "pdf.parse_excel_file()\n",
    "print(pdf.process_define_df.shape[0])\n",
    "print(pdf.pos)\n",
    "# print(pdf.process_define_df['Line Name'][0])\n",
    "pdf.pdf_add_pic()\n",
    "pdf.pdf_add_text()\n",
    "pdf.pad_add_out_rect()\n",
    "pdf.pdf_add_DL_cir()\n",
    "pdf.pdf_add_QPL_rect()\n",
    "pdf.pdf_add_others()\n",
    "pdf.pdf_save()"
   ],
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40\n",
      "[(36, 682.0), (103.5, 682.0), (171.0, 682.0), (238.5, 682.0), (306.0, 682.0), (373.5, 682.0), (441.0, 682.0), (508.5, 682.0), (36, 614.5), (103.5, 614.5), (171.0, 614.5), (238.5, 614.5), (306.0, 614.5), (373.5, 614.5), (441.0, 614.5), (508.5, 614.5), (36, 547.0), (103.5, 547.0), (171.0, 547.0), (238.5, 547.0), (306.0, 547.0), (373.5, 547.0), (441.0, 547.0), (508.5, 547.0), (36, 479.5), (103.5, 479.5), (171.0, 479.5), (238.5, 479.5), (306.0, 479.5), (373.5, 479.5), (441.0, 479.5), (508.5, 479.5), (36, 412.0), (103.5, 412.0), (171.0, 412.0), (238.5, 412.0), (306.0, 412.0), (373.5, 412.0), (441.0, 412.0), (508.5, 412.0)]\n",
      "Main Coil(Mono) Sub Line\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-23T06:15:23.830934Z",
     "start_time": "2024-07-23T06:15:23.829503Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for i in range(3):\n",
    "    print(i)"
   ],
   "id": "d184976fc23320f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-23T02:51:07.875985Z",
     "start_time": "2024-07-23T02:51:07.865110Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def wrap_text(text, width):  \n",
    "    \"\"\"  \n",
    "    将文本自动换行，以适应给定的宽度。  \n",
    "  \n",
    "    参数:  \n",
    "    text (str): 要换行的原始文本。  \n",
    "    width (int): 换行前的最大宽度（以字符为单位）。  \n",
    "  \n",
    "    返回:  \n",
    "    str: 换行后的文本。  \n",
    "    \"\"\"  \n",
    "    words = text.split()  # 将文本拆分为单词列表  \n",
    "    wrapped_lines = []  # 用于存储每行的列表  \n",
    "    current_line = []  # 当前正在构建的行  \n",
    "  \n",
    "    for word in words:  \n",
    "        print(word)\n",
    "        # 如果添加当前单词会导致当前行超过宽度限制，则先保存当前行  \n",
    "        if (len(' '.join(current_line + [word])) > width) and current_line:  \n",
    "            print('ss')\n",
    "            wrapped_lines.append(' '.join(current_line))  \n",
    "            current_line = [word]  # 开始新的一行  \n",
    "        else:  \n",
    "            current_line.append(word)  \n",
    "  \n",
    "    # 添加最后一行（如果有的话）  \n",
    "    if current_line:  \n",
    "        wrapped_lines.append(' '.join(current_line))  \n",
    "  \n",
    "    return '\\n'.join(wrapped_lines)  \n",
    "  \n",
    "# 示例  \n",
    "text = \"1-1 Coil winding\"  \n",
    "wrapped_text = wrap_text(text, 20)  \n",
    "print(wrapped_text)"
   ],
   "id": "5349432e07652a68",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1-1\n",
      "Coil\n",
      "winding\n",
      "1-1 Coil winding\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "5dc6b141f7bb3baa"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
